<?php

namespace App\Models;

use App\Services\PasswordService;

class User extends Model
{
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password'];

    // 用户状态
    const STATUS_DISABLE = 1;
    const STATUS_ENABLE = 2;
    const STATUS_ON_LEAVE = 3;

    public static function getUserAllScopes($userId)
    {
        /** @var User $userObj */
        $userObj = static::find($userId);

        $roleScopes = RoleScope::roleToScopes($userObj->getRoleId());

        $userScopes = [];

        foreach ($userScopes as $userScope)
        {
            if(in_array($userScope, $roleScopes)) {
                continue;
            }
            $roleScopes[] = $userScope;
        }
        return $roleScopes;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setMobile($mobile)
    {
        $this->mobile = $mobile;
    }

    public function getMobile()
    {
        return $this->mobile;
    }


    public function setPassword($password)
    {
        $this->password = PasswordService::hash($password);
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setSecret($secret)
    {
        $this->secret = $secret;
    }

    public function setRoleId($roleId)
    {
        $this->role_id = $roleId;
    }

    public function getRoleId()
    {
        return $this->role_id;
    }

    public function getRoleName()
    {
        return RoleScope::getRoleName($this->role_id);
    }

    public function  getCreatedAt()
    {
        return $this->created_at;
    }


    public function setStatus($status)
    {
        $this->status = $status;
    }

    public function getStatus()
    {
        return $this->status;
    }
}
